﻿using System;

using DevExpress.Xpo;

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;

namespace SpeedSCM.Module
{
    [NavigationItem("进货管理")]
    [ImageName("BO_Quote")]
    public class 进货退货单 : 单据
    {
        public 进货退货单(Session session)
            : base(session)
        {

        }
        protected override SeedType SeedType
        {
            get { return SeedType.进货退货单; }
        }
        private 往来单位 _收货单位;
        [Custom("Caption", "收货单位")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择收货单位")]
        public 往来单位 收货单位
        {
            get { return _收货单位; }
            set { SetPropertyValue<往来单位>("收货单位", ref _收货单位, value); }
        }

        private 仓库 _发货仓库;
        [Custom("Caption", "发货仓库")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择仓库")]
        public 仓库 发货仓库
        {
            get { return _发货仓库; }
            set { SetPropertyValue<仓库>("发货仓库", ref _发货仓库, value); }
        }

        private 银行账户 _收款账户;
        [Custom("Caption", "收款账户")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择收款账户")]
        public 银行账户 收款账户
        {
            get { return _收款账户; }
            set { SetPropertyValue<银行账户>("收款账户", ref _收款账户, value); }
        }
        private decimal _收款金额;
        [Custom("Caption", "收款金额")]
        public decimal 收款金额
        {
            get { return _收款金额; }
            set { SetPropertyValue<decimal>("收款金额", ref _收款金额, value); }
        }
        [Association("进货退货单-进货退货单明细")]
        public XPCollection<进货退货单明细> 进货退货单明细
        {
            get
            {
                return GetCollection<进货退货单明细>("进货退货单明细");
            }
        }
        public override bool Excute(IObjectSpace objectSpace)
        {
            foreach (进货退货单明细 item in 进货退货单明细)
            {
                库存 s = objectSpace.FindObject<库存>(CriteriaOperator.Parse("商品名称=? and 仓库名称=?", item.商品名称, 发货仓库));

                if (s != null)
                {
                    s.库存数量 -= item.数量;
                    s.进货总价 -= item.金额;
                    s.Save();
                }
            }
            objectSpace.CommitChanges();
            return base.Excute(objectSpace);
        }
    }
    [ImageName("BO_Quote")]
    [RuleCombinationOfPropertiesIsUnique(null, DefaultContexts.Save, "进货退货单,商品名称", CustomMessageTemplate = "进货退货单中已经存在该商品")]
    public class 进货退货单明细 : BaseObject
    {
        public 进货退货单明细(Session session)
            : base(session)
        {

        }
        private 进货退货单 _进货退货单;
        [Custom("Caption", "进货退货单")]
        [Association("进货退货单-进货退货单明细")]
        [RuleRequiredField]
        [Custom("AllowEdit", "False")]
        public 进货退货单 进货退货单
        {
            get { return _进货退货单; }
            set { SetPropertyValue<进货退货单>("进货退货单", ref _进货退货单, value); }
        }
        private 商品 _商品名称;
        [Custom("Caption", "商品名称")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择商品")]
        [Custom("ImmediatePostData", "True")]
        public 商品 商品名称
        {
            get { return _商品名称; }
            set
            {
                if (SetPropertyValue<商品>("商品名称", ref _商品名称, value))
                {
                    进货单价 = value.进货价;
                }
            }
        }
        private decimal _数量;
        [Custom("Caption", "数量")]
        [Custom("EditMask", "n2")]
        [Custom("DisplayFormat", "{0:n2}")]
        [RuleRequiredField]
        public decimal 数量
        {
            get { return _数量; }
            set { SetPropertyValue<decimal>("数量", ref _数量, value); }
        }
        private decimal _进货单价;
        [Custom("Caption", "进货单价")]
        public decimal 进货单价
        {
            get { return _进货单价; }
            set { SetPropertyValue<decimal>("进货单价", ref _进货单价, value); }
        }
        [Custom("Caption", "金额")]
        public decimal 金额
        {
            get { return 数量 * 进货单价; }
        }
        private string _备注;
        [Custom("Caption", "备注")]
        [Size(500)]
        public string 备注
        {
            get { return _备注; }
            set { SetPropertyValue<string>("备注", ref _备注, value); }
        }

    }
}
